package com.lm.activiti.boot.design_pattern.strategy.service;

import com.lm.activiti.boot.design_pattern.strategy.AttenHandler;
import com.lm.activiti.boot.design_pattern.strategy.annotate.AttenHandlerType;
import com.lm.activiti.boot.design_pattern.strategy.annotate.AttenHandlerTypeImpl;
import com.lm.activiti.boot.design_pattern.strategy.dto.AttenClockDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lm_jcf
 * @date 2020/6/22 17:35
 */
@Service
public class AttenService {

    public static final Logger LOG = LoggerFactory.getLogger(AttenService.class);

    private Map<AttenHandlerType, AttenHandler> attenHandlerMap;

    @Autowired
    public void setAttenHandlerMap(List<AttenHandler> attenHandlerList) {

        attenHandlerMap = attenHandlerList.stream().collect(
                Collectors.toMap(orderHandler -> AnnotationUtils.findAnnotation(orderHandler.getClass(), AttenHandlerType.class),
                        v -> v, (v1, v2) -> v1));

    }

    public void attenService(AttenClockDto attenClockDto) {

        AttenHandlerTypeImpl attenHandlerType = new AttenHandlerTypeImpl(String.valueOf(attenClockDto.getRoleType()));
        AttenHandler attenHandler = attenHandlerMap.get(attenHandlerType);
        String attenResult = attenHandler.clockHandle(attenClockDto);

        System.out.println(attenResult);
    }
}
